package com.ganjingcun.roma.web;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.*;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by GanJc on 2016/4/15.
 */
public class HelloShiRo {


    private static final Logger LOG = LoggerFactory.getLogger(HelloShiRo.class);


    public static void main(String[] args) {
        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //登录
        UsernamePasswordToken token = new UsernamePasswordToken("shiro", "201314");
        try{
            subject.login(token);
        }catch (AuthenticationException e){
            LOG.error("登录失败!",e);
            return;
        }
        LOG.info("登录成功!hello {}",subject.getPrincipal());

        //注销
        subject.logout();
    }
}
